package com.hzncc.flowable_diboot.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.diboot.core.binding.QueryBuilder;
import com.diboot.core.util.V;
import com.diboot.core.vo.Pagination;
import com.hzncc.flowable_diboot.dto.system.OaUserDTO;
import com.hzncc.flowable_diboot.entity.refundmanage.RefundRegister;
import com.hzncc.flowable_diboot.vo.refundmanage.RefundRegisterListVO;
import com.hzncc.flowable_diboot.vo.system.OaDeptListVO;
import com.hzncc.flowable_diboot.vo.system.OaUserListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.diboot.core.util.S;
import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.system.OaUser;

import com.hzncc.flowable_diboot.mapper.system.OaUserMapper;
import com.hzncc.flowable_diboot.service.system.OaUserService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
* OA人员 相关Service实现类
* @author MyName
* @version 1.0
* @date 2025-03-05
* Copyright © MyCorp
*/
@Slf4j
@Service
public class OaUserServiceImpl extends BaseServiceImpl<OaUserMapper, OaUser> implements OaUserService {
    @Override
    public List<OaUserListVO> getOaUserList(OaUserDTO queryDto, Pagination pagination) {
        QueryWrapper<OaUser> queryWrapper = QueryBuilder.toQueryWrapper(queryDto);
        if (pagination != null && V.isEmpty(pagination.getOrderBy())) {
            pagination.setOrderBy(Pagination.ORDER_BY_ID_DESC);
        }
        List<OaUserListVO> viewObjectList = this.getViewObjectList(queryWrapper, pagination, OaUserListVO.class);
        viewObjectList.forEach(oaUserListVO -> {
            oaUserListVO.setDepartmentFullName(getDepartmentFullPath(oaUserListVO));
        });
        return viewObjectList;
    }

    public String getDepartmentFullPath(OaUserListVO oaUserListVO) {
        List<String> departmentNames = new ArrayList<>();
        OaDeptListVO currentDepartment = oaUserListVO.getOaDept();

        while (currentDepartment != null) {
            if (!V.isEmpty(currentDepartment.getName())){
                departmentNames.add(currentDepartment.getName());
                currentDepartment = currentDepartment.getParentDept();
            }
        }

        // 反转列表以确保路径是从根部门到子部门
        Collections.reverse(departmentNames);

        // 使用分隔符连接部门名称
        return String.join("/", departmentNames);
    }
}